package com.example.tianling.service.Impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.tianling.dao.MatchMapper;
import com.example.tianling.entity.MatchEntity;
import com.example.tianling.entity.RoomEntity;
import com.example.tianling.entity.UserEntity;
import com.example.tianling.service.MatchService;
import org.springframework.stereotype.Service;
import org.w3c.dom.ls.LSInput;

import java.util.List;

@Service
public class MatchServiceImpl
        extends ServiceImpl<MatchMapper, MatchEntity>
        implements MatchService {

    /*
     * 匹配相关
     */
    @Override
    public void updateRealNumber(Integer roomId, Integer realNumber) {
        this.lambdaUpdate()
                .set(MatchEntity::getRealNumber, realNumber)
                .eq(MatchEntity::getRoomId, roomId)
                .update();
    }
    @Override
    public MatchEntity getMatch(String type) {
        MatchEntity match = this.lambdaQuery()
                .eq(MatchEntity::getType, type)
                .apply("real_number < max_number")
                .orderByDesc(MatchEntity::getRealNumber)
                .last("limit 1")
                .one();
        return match;
    }
    @Override
    public MatchEntity createMatch(RoomEntity room) {
        MatchEntity match = new MatchEntity();
        match.setRoomId(room.getRoomId());
        match.setRealNumber(1);
        match.setMaxNumber(room.getMaxNumber());
        match.setType(room.getType());
        baseMapper.insert(match);
        return match;
    }
    @Override
    public void removeByRoomId(Integer roomId) {
        QueryWrapper<MatchEntity> wrapper = new QueryWrapper<>();
        wrapper.eq("room_id", roomId);
        this.remove(wrapper);
    }



    // 根据room中的area、realNumber和maxNumber来查询match
    @Override
    public MatchEntity getMatchByRoom(RoomEntity room) {
        MatchEntity match = this.lambdaQuery()
//                .eq(MatchEntity::getArea, room.getArea())
                .eq(MatchEntity::getType, room.getType())
                .eq(MatchEntity::getIsMerge, 1)
                .le(MatchEntity::getRealNumber, room.getMaxNumber() - room.getRealNumber())
                .orderByDesc(MatchEntity::getRealNumber)
                .last("limit 1")
                .one();
        return match;
    }



    // 将房间插入匹配表
    @Override
    public void insertRoom(RoomEntity room) {
        MatchEntity match = new MatchEntity();
        match.setRoomId(room.getRoomId());
        match.setUserId(room.getUserId());
        System.out.println(room.getRealNumber());
        match.setRealNumber(room.getRealNumber());
        match.setMaxNumber(room.getMaxNumber());
        match.setArea(room.getArea());
        match.setPreference(room.getPreference());
        match.setType(room.getType());
        baseMapper.insert(match);
    }


    @Override
    public void removeByUserId(Integer userId) {
        QueryWrapper<MatchEntity> wrapper = new QueryWrapper<>();
        wrapper.eq("user_id", userId);
        this.remove(wrapper);
    }

    @Override
    public void updateIsMerge(Integer roomId, Integer isMerge) {
        this.lambdaUpdate()
                .set(MatchEntity::getIsMerge, isMerge)
                .eq(MatchEntity::getRoomId, roomId)
                .update();
    }

}
